<?php
namespace App\Modules\Admin\Controllers;

use App\Http\Resources\Admin\RoleOptionResource;
use App\Http\Resources\Admin\RoleResource;
use App\Modules\Admin\Services\RoleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class RoleController extends AdminController
{
    public function index(Request $request, RoleService $service)
    {
        $roles = RoleResource::collection($service->page($request->all()));

        return $this->responseSuccess([
            'list' => $roles,
            'total' => $roles->total()
        ]);
    }

    public function add(Request $request, RoleService $service)
    {
        return $this->responseSuccess($service->add($request->all()));
    }

    public function edit(Request $request, Int $id, RoleService $service)
    {
        return $this->responseSuccess($service->edit($id, $request->all()));

    }

    public function detail(Int $id, RoleService $service)
    {
        return $this->responseSuccess(new RoleResource($service->detail($id)));

    }

    public function del(Int|String $id, RoleService $service)
    {
        return $this->responseSuccess($service->del($id));
    }

    public function options(RoleService $service)
    {
        return $this->responseSuccess(RoleOptionResource::collection($service->options()));
    }

    public function menuIds(Int $roleId, RoleService $service)
    {
        Log::info('role id: '. $roleId);
        return $this->responseSuccess($service->getRoleMenuIds($roleId));
    }

    public function menus(Int $roleId, RoleService $service, Request $request)
    {
        // return $this->responseSuccess($request->all());
        return $this->responseSuccess($service->assignPermission($roleId, $request->all()));
    }
}